package com.snailvr.tv.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.snail.readyvr.R;
import com.snail.readyvr.util.EventBus;
import com.snailvr.tv.Definition;
import com.snailvr.tv.event.GifEvent;
import com.snailvr.tv.ui.data.DefGifticons;

import java.util.List;

/**
 * Created by Administrator on 2017/1/9.
 */

public class GiftGridAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private int mSize;
    private List<String> mIconStringsList;


    public GiftGridAdapter(Context context, List<String> icon_str_list){
        super();
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mSize = icon_str_list.size();
        mIconStringsList = icon_str_list;
    }

    @Override
    public int getCount() {
        return mSize;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.gift_item,null,false);
            viewHolder = new ViewHolder();
            viewHolder.icon = (ImageView)convertView.findViewById(R.id.img_view_gift_item);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.icon.setImageResource(DefGifticons.giftHashMap.get(mIconStringsList.get(position)));

        viewHolder.icon.setTag(mIconStringsList.get(position));

        viewHolder.icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // emit event with string to edittext
                EventBus.getDefault().post(Definition.BusTag.GIFT_TAG, new GifEvent(mIconStringsList.get(position)));
            }
        });


        return convertView;
    }

    static class ViewHolder {
        ImageView icon;
    }


}
